//
//  UIImageView+ZBSDWebImage.m
//  FunctionModule
//
//  Created by mac03 on 2021/6/9.
//

#import "UIImageView+ZBSDWebImage.h"

@implementation UIImageView (ZBSDWebImage)
- (void)downloadImage:(NSString *)urlStr placeholder:(NSString *)imageName {
    if (![urlStr isKindOfClass:[NSString class]]) {
        [self sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:imageName] options:SDWebImageRetryFailed|SDWebImageLowPriority];
        return;
    }
    if ([urlStr containsString:@"http"]||[urlStr containsString:@"https"]) {
        [self sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:imageName] options:SDWebImageRetryFailed|SDWebImageLowPriority];
    } else {
        self.image = [UIImage imageNamed:imageName];
        return;
    }
}

- (void)downloadImage:(NSString *)urlStr placeholder:(NSString *)imageName success:(DownImageSuccessBlock)success failed:(DownImageFailedBlock)failed progress:(DownImageProgressBlock)progress {
    
    [self sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:imageName] options:SDWebImageRetryFailed|SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL)  {
        
        progress(receivedSize * 1.0 / expectedSize);
        
    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        if (error) {
            failed(error);
        } else {
            self.image = image;
            success(image);
        }
        
    }];
}
@end
